<?php

namespace common\traits;

use kartik\tree\models\TreeTrait;

/**
 * Class TreeModelTrait
 *
 * @property integer id
 * @property integer lvl
 * @property string name
 * @package common\traits
 */
trait TreeModelTrait
{

    use TreeTrait
    {
        isDisabled as parentIsDisabled;
    }

    /**
     * @param string $padding
     * @param array  $where
     * @return array
     */
    public static function getTreeList($padding = '　', $where = [])
    {
        /** @var static[] $groups */
        $groups = self::find()
            ->where($where)
            ->orderBy([
                'root' => SORT_ASC,
                'lvl' => SORT_ASC,
                'lft' => SORT_ASC,
            ])
            ->all();

        $result = [];
        foreach ($groups as $group)
        {
            $result[$group->id] = str_repeat($padding, $group->lvl) . $group->name;
        }

        return $result;
    }

    /**
     * @param string $padding
     * @param array  $where
     * @return array
     */
    public static function getTreeObjects($padding = '　', $where = [])
    {
        /** @var static[] $groups */
        $groups = self::find()
            ->where($where)
            ->orderBy([
                'root' => SORT_ASC,
                'lvl' => SORT_ASC,
                'lft' => SORT_ASC,
            ])
            ->all();

        $result = [];
        foreach ($groups as $group)
        {
            $result[str_repeat($padding, $group->lvl) . $group->name] = $group;
        }

        return $result;
    }
}
